package com.richard.base.net.utils

import okhttp3.internal.and
import java.io.UnsupportedEncodingException
import java.security.MessageDigest
import java.security.NoSuchAlgorithmException

object EncodeUtils {

    /**
     * @param string
     * @return
     */
    fun md5(string: String): String? {
        val hash: ByteArray
        hash = try {
            MessageDigest.getInstance("MD5").digest(
                string.toByteArray(charset("UTF-8"))
            )
        } catch (e: NoSuchAlgorithmException) {
            throw RuntimeException("Huh, MD5 should be supported?", e)
        } catch (e: UnsupportedEncodingException) {
            throw RuntimeException("Huh, UTF-8 should be supported?", e)
        }
        val hex = StringBuilder(hash.size * 2)
        for (b in hash) {
            if (b and 0xFF < 0x10) {
                hex.append("0")
            }
            hex.append(Integer.toHexString(b and 0xFF))
        }
        return hex.toString().toUpperCase()
    }
}